package com.cheyunkeji.er.app;

import android.content.Context;
import android.text.TextUtils;

import com.cheyunkeji.er.bean.PushMsgBean;
import com.google.gson.Gson;

/**
 * Created by Michael on 2017/8/15 9:54.
 *
 * 用于管理推送消息（保存、获取）的管理器
 */

public class PushMsgManager extends MyPreferencesManager {

    public static final int MSG_TYPE_XUNJIA = 1;
    public static final int MSG_TYPE_YUZHAN = 2;
    public static final int MSG_TYPE_PAIMAI = 3;
    public static final int MSG_TYPE_ZHONGBIAO = 4;
    public static final int MSG_TYPE_LIUBIAO = 5;
    public static final int MSG_TYPE_QUANMIAN = 6;
    public static final int MSG_TYPE_ZHANNEIXIN = 7;

    public static String NOTICE_TYPE_XUNJIA = "xunjia";
    public static String NOTICE_TYPE_XUNJIA_MSG_COUNT = NOTICE_TYPE_XUNJIA + "_msg_count";

    public static String NOTICE_TYPE_YUZHAN = "yuzhan";
    public static String NOTICE_TYPE_YUZHAN_MSG_COUNT = NOTICE_TYPE_YUZHAN + "_msg_count";

    public static String NOTICE_TYPE_PAIMAI = "paimai";
    public static String NOTICE_TYPE_PAIMAI_MSG_COUNT = NOTICE_TYPE_PAIMAI + "_msg_count";

    public static String NOTICE_TYPE_ZHONGBIAO = "zhongbiao";
    public static String NOTICE_TYPE_ZHONGBIAO_MSG_COUNT = NOTICE_TYPE_ZHONGBIAO + "_msg_count";

    public static String NOTICE_TYPE_LIUBIAO = "liubiao";
    public static String NOTICE_TYPE_LIUBIAO_MSG_COUNT = NOTICE_TYPE_LIUBIAO + "_msg_count";

    public static String NOTICE_TYPE_QUANMIAN_PINGGU = "quanmian_pinggu";
    public static String NOTICE_TYPE_QUANMIAN_PINGGU_MSG_COUNT = NOTICE_TYPE_QUANMIAN_PINGGU + "_msg_count";

    public static String NOTICE_TYPE_ZHANNEIXIN = "zhanneixin";
    public static String NOTICE_TYPE_ZHANNEIXIN_MSG_COUNT = NOTICE_TYPE_ZHANNEIXIN + "_msg_count";

    private static PushMsgManager mInstance = null;
    private static String currentFileName = null;

    public PushMsgManager(Context context) {
        super(context);
    }

    public PushMsgManager(Context context, String fileName) {
        super(context, fileName);
    }


    public static PushMsgManager getInstance(Context context, String fileName) {
        synchronized (PushMsgManager.class) {
            if (mInstance == null || !TextUtils.equals(fileName, currentFileName)) {
                mInstance = new PushMsgManager(context, fileName);
                currentFileName = fileName;
            }
        }
        return mInstance;
    }


    public void clearNotifyMsgRecordCount(int msgType) {

        switch (msgType) {

            case MSG_TYPE_XUNJIA:

                putInt(NOTICE_TYPE_XUNJIA_MSG_COUNT, 0);
                break;

            case MSG_TYPE_YUZHAN:
                putInt(NOTICE_TYPE_YUZHAN_MSG_COUNT, 0);
                break;

            case MSG_TYPE_PAIMAI:
                putInt(NOTICE_TYPE_PAIMAI_MSG_COUNT, 0);
                break;

            case MSG_TYPE_ZHONGBIAO:
                putInt(NOTICE_TYPE_ZHONGBIAO_MSG_COUNT, 0);
                break;

            case MSG_TYPE_LIUBIAO:
                putInt(NOTICE_TYPE_LIUBIAO_MSG_COUNT, 0);
                break;

            case MSG_TYPE_QUANMIAN:
                putInt(NOTICE_TYPE_QUANMIAN_PINGGU_MSG_COUNT, 0);
                break;

            case MSG_TYPE_ZHANNEIXIN:
                putInt(NOTICE_TYPE_ZHANNEIXIN_MSG_COUNT, 0);
                break;

            default:
                break;
        }

    }

    public void saveMsgContent(int type, String msgContent) {

        switch (type) {
            case MSG_TYPE_XUNJIA:
                putString(NOTICE_TYPE_XUNJIA, msgContent);
                setMsgCount(NOTICE_TYPE_XUNJIA_MSG_COUNT);
                break;
            case MSG_TYPE_YUZHAN:
                putString(NOTICE_TYPE_YUZHAN, msgContent);
                setMsgCount(NOTICE_TYPE_YUZHAN_MSG_COUNT);
                break;
            case MSG_TYPE_PAIMAI:
                putString(NOTICE_TYPE_PAIMAI, msgContent);
                setMsgCount(NOTICE_TYPE_PAIMAI_MSG_COUNT);
                break;
            case MSG_TYPE_ZHONGBIAO:
                putString(NOTICE_TYPE_ZHONGBIAO, msgContent);
                setMsgCount(NOTICE_TYPE_ZHONGBIAO_MSG_COUNT);
                break;
            case MSG_TYPE_LIUBIAO:
                putString(NOTICE_TYPE_LIUBIAO, msgContent);
                setMsgCount(NOTICE_TYPE_LIUBIAO_MSG_COUNT);
                break;
            case MSG_TYPE_QUANMIAN:
                putString(NOTICE_TYPE_QUANMIAN_PINGGU, msgContent);
                setMsgCount(NOTICE_TYPE_QUANMIAN_PINGGU_MSG_COUNT);
                break;
            case MSG_TYPE_ZHANNEIXIN:
                putString(NOTICE_TYPE_ZHANNEIXIN, msgContent);
                setMsgCount(NOTICE_TYPE_ZHANNEIXIN_MSG_COUNT);
                break;
            default:
                break;
        }
    }

    private void setMsgCount(String msgCountKey) {

        int previousCount = getInt(msgCountKey, 0);
        putInt(msgCountKey, ++previousCount);

    }

    public int getMsgCount(int msgType) {

        int msgCount = 0;

        switch (msgType) {
            case MSG_TYPE_XUNJIA:
                msgCount = getInt(NOTICE_TYPE_XUNJIA_MSG_COUNT, 0);
                break;
            case MSG_TYPE_YUZHAN:
                msgCount = getInt(NOTICE_TYPE_YUZHAN_MSG_COUNT, 0);
                break;
            case MSG_TYPE_PAIMAI:
                msgCount = getInt(NOTICE_TYPE_PAIMAI_MSG_COUNT, 0);
                break;
            case MSG_TYPE_ZHONGBIAO:
                msgCount = getInt(NOTICE_TYPE_ZHONGBIAO_MSG_COUNT, 0);
                break;
            case MSG_TYPE_LIUBIAO:
                msgCount = getInt(NOTICE_TYPE_LIUBIAO_MSG_COUNT, 0);
                break;
            case MSG_TYPE_QUANMIAN:
                msgCount = getInt(NOTICE_TYPE_QUANMIAN_PINGGU_MSG_COUNT, 0);
                break;
            case MSG_TYPE_ZHANNEIXIN:
                msgCount = getInt(NOTICE_TYPE_ZHANNEIXIN_MSG_COUNT, 0);
                break;

            default:
                break;
        }
        return msgCount;
    }

    public PushMsgBean getLatestMsgEntity(int msgType) {

        String jsonString = null;

        switch (msgType) {
            case MSG_TYPE_XUNJIA:
                jsonString = getString(NOTICE_TYPE_XUNJIA, "");
                break;
            case MSG_TYPE_YUZHAN:
                jsonString = getString(NOTICE_TYPE_YUZHAN, "");
                break;
            case MSG_TYPE_PAIMAI:
                jsonString = getString(NOTICE_TYPE_PAIMAI, "");
                break;
            case MSG_TYPE_ZHONGBIAO:
                jsonString = getString(NOTICE_TYPE_ZHONGBIAO, "");
                break;
            case MSG_TYPE_LIUBIAO:
                jsonString = getString(NOTICE_TYPE_LIUBIAO, "");
                break;
            case MSG_TYPE_QUANMIAN:
                jsonString = getString(NOTICE_TYPE_QUANMIAN_PINGGU, "");
                break;
            case MSG_TYPE_ZHANNEIXIN:
                jsonString = getString(NOTICE_TYPE_ZHANNEIXIN, "");
                break;

            default:
                break;
        }

        if (!TextUtils.isEmpty(jsonString)) {
            return new Gson().fromJson(jsonString, PushMsgBean.class);
        } else {
            return null;
        }
    }
}


